

def multiplyByDigit(x, digit_y) #x is an array of digits(ints)
  #return "error in multiplyByDigit" if x.class != Array
  result = [];
  carrier = 0;
  x.reverse_each do |digit_x|
    multi = digit_x * digit_y + carrier;
    result.insert(0, multi % 10)
    carrier = (multi / 10) .to_i
  end
  result.insert(0, carrier) if carrier > 0
  return result
end

def thirdGradeMultiplication(x, y)#x and y are arrays of digits(ints)
  partials = []
  zeropad = []
  y.reverse_each do |digit_y|
    partials.push(multiplyByDigit(x, digit_y).concat(zeropad))
    zeropad.concat([0])
  end
  #summing partials
  result = 0;
  partials.each do |item|
    result += item.join.to_i
  end
return result.to_s
end

def karatsuba(x, y) #inputs are arrays of digits(ints) each of size n
end


x = "3343".split(//).collect do |digit|; digit.to_i; end
y = "987534".split(//).collect do |digit|; digit.to_i; end

p thirdGradeMultiplication(x,y)
